/**
 * Created by KY on 2017/9/30.
 */
define(['ldfw/core','bscroll'],function(LDFW,BScroll){
    return LDFW.Component.extend({
        initialize:function(){
            this.scroller = new BScroll(this.el,{
                probeType:1,
                click:true,
                scrollY: true,
                pullDownRefresh:{
                    threshold: 50,
                    stop: 40
                },
                scrollbar:false,
                bindToWrapper:true
            })
            this.beforePullDown = true
            this.isRebounding = false
            this.scroller.on('pullingDown', () => {
                this.beforePullDown = false
                this.isPullingDown = true
                this.pulling = true
                this.trigger('pullingDown')
                this.$el.find('.refresh-hook').html('<i class="loading"></i>正在刷新')
            })
            this.scroller.on('scroll', (pos) => {
                if (this.beforePullDown){
                    if(pos.y > 50){
                        this.$el.find('.refresh-hook').text('释放刷新')
                    }
                }
            })
        },
        forceUpdate:function(){
            if (this.isPullingDown) {
                this.pulling = false
                this.reboundPullDown().then(() => {
                    this.afterPullDown()
                })
            } else {
                this.scroller.refresh()
            }
        },
        reboundPullDown() {
            return new Promise((resolve) => {
                setTimeout(() => {
                    this.isRebounding = true
                    this.isPullingDown = false
                    $('.refresh-hook').text('刷新成功')
                    this.scroller.finishPullDown()
                    resolve()
                }, 600)
            })
        },
        afterPullDown() {
            setTimeout(() => {
                this.beforePullDown = true
                this.isRebounding = false
                $('.refresh-hook').text('释放刷新')
                this.scroller.refresh()
            }, 600)
        },
        refresh(){
            this.scroller.refresh()
        }
    })
})